package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.employee.template.EmployeeTemplate;
import com.xcity.game.item.ItemTemplate;
import com.xcity.game.player.PlayerUtils;
import com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeInfo;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class EmployeeListResponse extends Response {

//	public byte type;
//	public int size;
	public int max;
	public List<EmployeeInfo> list = Collections.emptyList();
	public List<Piece> pieces = Collections.emptyList();
	
	public int page; // 当前页数
	public int maxPage; // 最大页数
	
	public static class Piece {
		private EmployeeTemplate template;
		private ItemTemplate pieceItem;
		private int ownedPieceCount;
		public Piece(Player player, EmployeeTemplate template) {
			this.template = template;
			this.pieceItem = App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, template.getPieceId());
			this.ownedPieceCount = player.getBag().countOf(template.getPieceId());
		}
		public int getId() {
			return template.getId();
		}
		public String getName() {
			return template.getName();
		}
		public String getIcon() {
			return template.getIcon();
		}
		public byte getSex() {
			return template.getSex();
		}
		public byte getQuality() {
			return pieceItem.getQuality();
		}
		public int getPieceId() {
			return pieceItem.getId();
		}
		public int getPieceNeedCount() {
			return template.getPieceCount();
		}
		public int getPieceCount() {
			return ownedPieceCount;
		}
	}
	
//	public EmployeeListResponse(int serial, Employee employee, Player player) {
//		this(serial, (byte) -1, Arrays.asList(employee), player);
//	}
//	
//	public EmployeeListResponse(int serial, byte type, Collection<Employee> list, Player player) {
//		this(serial, type, list, player, null);
//	}
	
	public EmployeeListResponse(int serial, Player player, Map<Integer, EmployeeTemplate> piecesMap) {
		this(serial, null, 1, 1, player, piecesMap);
	}
	
	public EmployeeListResponse(int serial, Collection<Employee> list, int page, int maxPage, Player player, Map<Integer, EmployeeTemplate> piecesMap) {
		super(serial);
//		this.type = type;
		if (list != null && list.size() > 0) {
			this.list = new ArrayList<EmployeeInfo>(list.size());
			for (Employee employee : list) {
				this.list.add(new EmployeeInfo(employee));
			}
		}
		this.max = PlayerUtils.getEmployeeCapcity(player);
		if (piecesMap != null) {
			Collection<EmployeeTemplate> pieceTemps = piecesMap.values();
			this.pieces = new ArrayList<>(piecesMap.size());
			for (EmployeeTemplate temp : pieceTemps) {
				Piece p  = new Piece(player, temp);
				this.pieces.add(p);
			}
		}
		this.page = page;
		this.maxPage = maxPage;
	}

}
